package com.mukefile.service.aop;

import com.mukefile.dao.LogDao;
import com.mukefile.pojo.Log;
import com.mukefile.pojo.MukeFile;
import com.mukefile.pojo.User;
import com.mukefile.dao.impl.LogDaoImpl;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: kenewstar
 * @Description: 日志操作切面类
 * @Date:Created in 2020/9/11
 */
@Component
@Aspect
public class LogAop {
    @Autowired
    private LogDao logDao;

    /**
     * 用户登录切面
     */
    @AfterReturning(value = "execution(* com.mukefile.service.impl.UserServiceImpl.userLogin(..))",
            returning ="user" )
    public void userLoginLog(User user){
        if (Objects.isNull(user)){
            return;
        }
        // 用户登录成功
        // 创建日志类对象
        Log log = new Log();
        // 填充日志对象
        log.setLogContent("用户："+user.getUsername()+" 登录了系统!");
        log.setLogTime(new Date());
        log.setUid(user.getId());
        // 执行添加日志操作

        logDao.insertLog(log);
        System.out.println(user);

    }

    /**
     * 用户退出操作
     * @param map
     */
    @Before(value = "execution(* com.mukefile.service.impl.UserServiceImpl.userExit(..))&&args(map)")
    public void userExitLog(Map map){
       User user = (User)map.get("user");
        if (Objects.isNull(user)){
            return;
        }
        Log log = new Log();
        log.setLogContent("用户："+user.getUsername()+" 退出了系统！");
        log.setLogTime(new Date());
        log.setUid(user.getId());
        // 添加日志操作
        this.logDao.insertLog(log);
    }

    /**
     * 修改密码日志操作
     */
    @AfterReturning(value = "execution(int com.mukefile.service.impl.UserServiceImpl.updatePwd(" +
            "String,com.mukefile.pojo.User))&&args(pwd,user))",
            returning = "result")
    public void updatePwdLog(int result,User user,String pwd){
        if (Objects.isNull(user)){
            return;
        }
        Log log = new Log();
        // 用户修改了密码
        if (result == 0){
            // 修改失败
            log.setLogContent("用户："+user.getUsername()+" 修改密码失败!");
        }else {
            log.setLogContent("用户："+user.getUsername()+" 修改密码成功!");
        }
        log.setLogTime(new Date());
        log.setUid(user.getId());
        // 执行日志插入操作
        logDao.insertLog(log);
    }

    /**
     * 文件搜索日志
     * @param key 关键字
     * @param user 用户对象
     */
    @Before(value = "execution(* com.mukefile.service.impl.UserServiceImpl.searchFile(..))&&" +
            "args(key,user)")
    public void searchFileLog(String key,User user){
        if (Objects.isNull(user)) return;
        // 创建日志对象
        Log log = new Log();
        log.setLogContent("用户："+user.getUsername()+" 搜索了关键词为《"+key+"》的文件!");
        log.setLogTime(new Date());
        log.setUid(user.getId());
        // 执行插入
        logDao.insertLog(log);
    }
    /**
     * ***********************************************
     * 用户对文件操作的切面日志
     * ***********************************************
     */

    /**
     * 用户上传文件操作的日志
     */
    @AfterReturning(value = "execution(int com.mukefile.service.impl.MukeFileServiceImpl.fileUpload(..))" +
            "&&args(file,mukeFile)",returning = "result", argNames = "file,mukeFile,result")
    public void myFileUploadLog(File file, MukeFile mukeFile,int result){

        if (Objects.isNull(mukeFile)) return;

        Log log = new Log();
        log.setLogTime(new Date());
        if (result>0){
            log.setLogContent("用户："+mukeFile.getUser().getUsername()+" 上传文件《"
                    +mukeFile.getFileName()+"》成功!");
        }else {
            log.setLogContent("用户："+mukeFile.getUser().getUsername()+" 上传文件《"
                    +mukeFile.getFileName()+"》失败!");
        }
        log.setUid(mukeFile.getUser().getId());
        // 执行插入方法
        logDao.insertLog(log);
    }

    /**
     * 用户下载文件日志
     * @param user
     * @param filename
     */
    @After(value = "execution(* com.mukefile.service.impl.MukeFileServiceImpl.download(..))" +
            "&&args(user,filename)", argNames = "user,filename")
    public void myDownloadLog(User user,String filename){
        if (Objects.isNull(user)) return;
        // 创建日志对象
        Log log = new Log();
        // 填充日志
        log.setLogTime(new Date());
        log.setLogContent("用户："+user.getUsername()+" 下载了文件《"+filename+"》!");
        log.setUid(user.getId());
        // 执行插入
        logDao.insertLog(log);

    }

    /**
     * 用户删除文件日志操作
     * @param mukefile
     * @param result
     */
    @AfterReturning(value = "execution(int com.mukefile.service.impl.MukeFileServiceImpl.deleteFile(..))" +
            "&&args(mukefile)",returning = "result", argNames = "mukefile,result")
    public void myDeleteFileLog(MukeFile mukefile,int result){
        if (Objects.isNull(mukefile)) return;
        // 创建日志对象
        Log log = new Log();
        // 填充日志对象
        if (result>0){
            log.setLogContent("用户："+mukefile.getUser().getUsername()+" 删除文件《"+
                    mukefile.getFileName()+"》成功!");
        }else {
            log.setLogContent("用户："+mukefile.getUser().getUsername()+" 删除文件《"+
                    mukefile.getFileName()+"》失败!");
        }
        log.setLogTime(new Date());
        log.setUid(mukefile.getUser().getId());
        // 执行插入
        logDao.insertLog(log);

    }

}
